package com.ziytek.web.citizen.model.req;

import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.util.StringUtil;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * @author fenghx
 * @version v1.0
 * @className QueryBatteriesD
 * @descripton 查询充放电电流电压详情数据
 * @date 2023/4/20 14:23
 **/
@Data
@Slf4j
public class BatteryListQueryReq {

    /**
     * 机柜编号
     */
    @NotBlank(message = "机柜编号不能为空")
    private String cabinetNo;

    /**
     * A、B面
     */
    private String side;

    /**
     * 左右门 l-左门 r-右门
     */
    private String door;

    /**
     * 层数
     */
    private String tier;

    /**
     * 通道
     */
    private String position;

    public boolean requestCheck() {
        return StringUtil.isNotEmpty(this.cabinetNo);
    }

    public String requestCheckAndResult() {
        // 通道不为空而且数据正确
        boolean positionNumNormal = StringUtil.isNotEmpty(this.position) && NumberUtils.isCreatable(this.position);
        boolean positionNum = (NumberUtils.toInt(this.position) >= NumConstant.INT_ONE && NumberUtils.toInt(this.position) <= NumConstant.INT_TEN_SIX);
        boolean positionNormal = positionNumNormal && positionNum;
        boolean tierNumNormal = StringUtil.isNotEmpty(this.tier) && NumberUtils.isCreatable(this.tier);
        boolean tierNum = (NumberUtils.toInt(this.tier) >= NumConstant.INT_ONE && NumberUtils.toInt(this.tier) <= NumConstant.INT_EIGHT);
        boolean tierNormal = tierNumNormal && tierNum;
        boolean doorNormal = StringUtil.isNotEmpty(this.door) && (ServerConstant.DOOR_L.equalsIgnoreCase(this.door) || ServerConstant.DOOR_R.equalsIgnoreCase(this.door));
        boolean sideNormal = StringUtil.isNotEmpty(this.side) && (ServerConstant.SIDE_A.equalsIgnoreCase(this.side) || ServerConstant.SIDE_B.equalsIgnoreCase(this.side));

        if (positionNumNormal && !positionNum) {
            return "通道传值错误";
        }

        // 通道有数据，层数、门、面都必须有数据而且要正常
        if (positionNormal) {
            return tierNormal && doorNormal && sideNormal ? "" : "AB面、左右门、层数传值错误";
        }

        if (tierNumNormal && !tierNum) {
            return "层数传值错误";
        }

        // 层数有数据，门、面都必须有数据而且要正常
        if (tierNormal) {
            return doorNormal && sideNormal ? "" : "AB面、左右门传值错误";
        }

        // 门有数据，面都必须有数据而且要正常
        if (doorNormal) {
            return sideNormal ? "" : "AB面传值错误";
        }

//        // 面传值
//        if (!sideNormal) {
//            return "AB面传值错误";
//        }

        return "";
    }


    public boolean isPositionNormal() {
        boolean positionNormal = StringUtil.isNotEmpty(this.position) && NumberUtils.isCreatable(this.tier) && (NumberUtils.toInt(this.position) >= NumConstant.INT_ONE && NumberUtils.toInt(this.position) <= NumConstant.INT_TEN_SIX);
        boolean tierNormal = StringUtil.isNotEmpty(this.tier) && NumberUtils.isCreatable(this.tier) && (NumberUtils.toInt(this.tier) >= NumConstant.INT_ONE && NumberUtils.toInt(this.tier) <= NumConstant.INT_EIGHT);
        boolean doorNormal = StringUtil.isNotEmpty(this.door) && (ServerConstant.DOOR_L.equalsIgnoreCase(this.door) || ServerConstant.DOOR_R.equalsIgnoreCase(this.door));
        boolean sideNormal = StringUtil.isNotEmpty(this.side) && (ServerConstant.SIDE_A.equalsIgnoreCase(this.side) || ServerConstant.SIDE_B.equalsIgnoreCase(this.side));

        if (positionNormal) {
            return tierNormal && doorNormal && sideNormal;
        }
        return false;
    }

    public boolean isTierNormal() {
        boolean tierNormal = StringUtil.isNotEmpty(this.tier) && NumberUtils.isCreatable(this.tier) && (NumberUtils.toInt(this.tier) >= NumConstant.INT_ONE && NumberUtils.toInt(this.tier) <= NumConstant.INT_EIGHT);
        boolean doorNormal = StringUtil.isNotEmpty(this.door) && (ServerConstant.DOOR_L.equalsIgnoreCase(this.door) || ServerConstant.DOOR_R.equalsIgnoreCase(this.door));
        boolean sideNormal = StringUtil.isNotEmpty(this.side) && (ServerConstant.SIDE_A.equalsIgnoreCase(this.side) || ServerConstant.SIDE_B.equalsIgnoreCase(this.side));

        // 层数有数据，门、面都必须有数据而且要正常
        if (tierNormal) {
            return doorNormal && sideNormal;
        }
        return false;
    }

    public boolean isDoorNormal() {
        boolean doorNormal = StringUtil.isNotEmpty(this.door) && (ServerConstant.DOOR_L.equalsIgnoreCase(this.door) || ServerConstant.DOOR_R.equalsIgnoreCase(this.door));
        boolean sideNormal = StringUtil.isNotEmpty(this.side) && (ServerConstant.SIDE_A.equalsIgnoreCase(this.side) || ServerConstant.SIDE_B.equalsIgnoreCase(this.side));
        if (doorNormal) {
            return sideNormal;
        }
        return false;
    }

    public boolean isSideNormal() {
        return StringUtil.isNotEmpty(this.side) && (ServerConstant.SIDE_A.equalsIgnoreCase(this.side) || ServerConstant.SIDE_B.equalsIgnoreCase(this.side));
    }
}
